package friendly_link

import (
	"errors"
	"hft/Database/admin"
	"hft/Database/admin/friendly_link"
	"hft/Http/Models/dao"
	"hft/Http/Services/friendly_link/friendly_link_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"

	"gorm.io/gorm"
)

type FriendlyLink struct{}

// 获取友情链接
func (*FriendlyLink) GetFriendlyLink(paginate *utils.Page) *utils.Page {
	var list = make([]*friendly_link.GloFriendlyLink, 0)
	global.OC_DB.Scopes(dao.Paginate(dao.Page(*paginate), global.OC_DB)).Order("sort desc").Find(&list).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)
	paginate.Data = list
	return paginate
}

// 创建友情链接
func (*FriendlyLink) CreateFriendlyLink(data *friendly_link.GloFriendlyLink) (err error) {
	err = global.OC_DB.Transaction(func(tx *gorm.DB) error {
		if err = tx.Create(&data).Error; err != nil {
			return errors.New(e.ErrorInfo)
		}
		// 同时为所有的城市都添加友情链接
		var cities []*admin.Region
		tx.Model(&admin.Region{}).Select("manage_id,id").Where("manage_id IS NOT NULL").Find(&cities)
		var newCitiesLinks []*friendly_link.CityFriendLyLink
		for _, v := range cities {
			newCitiesLinks = append(newCitiesLinks, &friendly_link.CityFriendLyLink{Title: data.Title, Link: data.Link, Code: int(data.ID), City: int(v.ID)})
		}
		tx.Create(&newCitiesLinks)
		return nil
	})

	return err
}

// 编辑链接
func (*FriendlyLink) Edit(data *friendly_link.GloFriendlyLink) (err error) {
	err = global.OC_DB.Transaction(func(tx *gorm.DB) error {
		if err = tx.Model(&friendly_link.GloFriendlyLink{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
			"title": data.Title,
			"sort":  data.Sort,
			"link":  data.Sort,
		}).Error; err != nil {
			return errors.New(e.ErrorInfo)
		}
		// 同时更新城市对应的code
		if res := tx.Model(&friendly_link.CityFriendLyLink{}).Where("code = ?", data.ID).Updates(map[string]interface{}{
			"title": data.Title,
			"link":  data.Link,
		}); res.Error != nil || res.RowsAffected == 0 {
			return errors.New(e.ErrorInfo)
		}
		return nil
	})
	return err
}

// 删除链接
func (*FriendlyLink) Delete(ids []int) (err error) {
	if err = global.OC_DB.Unscoped().Delete(&friendly_link.GloFriendlyLink{}, ids).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

// 获取城市的友情列表
func (*FriendlyLink) GetCityLink(paginate *friendly_link_model.FriendlyCityLinkSearch) *utils.Page {
	var list = make([]*friendly_link.CityFriendLyLink, 0)
	global.OC_DB.Where("city = ?", paginate.ID).Scopes(dao.Paginate(dao.Page(paginate.Page), global.OC_DB)).Order("sort desc").Find(&list).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)
	paginate.Data = list
	return &paginate.Page
}

// 创建城市友情链接
func (*FriendlyLink) CreateCityFriendlyLink(data *friendly_link.CityFriendLyLink) (err error) {
	if data.City == 0 {
		return errors.New(e.RegionNotFound)
	}
	var region *admin.Region
	global.OC_DB.First(&region, data.City)
	if region.Level != "city" {
		return errors.New(e.ErrorInfo)
	}
	if err = global.OC_DB.Model(&friendly_link.CityFriendLyLink{}).Create(map[string]interface{}{
		"sort":  data.Sort,
		"title": data.Title,
		"link":  data.Link,
		"city":  data.City,
	}).Error; err != nil {
		return errors.New(e.ErrorInfo)
	} else {
		return nil
	}
}

// 编辑城市友情链接
func (*FriendlyLink) EditCityFriendlyLink(data *friendly_link.CityFriendLyLink) (err error) {
	if data.City == 0 {
		return errors.New(e.RegionNotFound)
	}
	var region *admin.Region
	global.OC_DB.First(&region, data.City)
	if region.Level != "city" {
		return errors.New(e.ErrorInfo)
	}
	if err = global.OC_DB.Model(&friendly_link.CityFriendLyLink{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"sort":  data.ID,
		"title": data.Title,
		"link":  data.Link,
	}).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

// 删除友情链接
func (*FriendlyLink) DeleteCityFriendlyLink(ids []int) (err error) {
	if err = global.OC_DB.Unscoped().Delete(&friendly_link.CityFriendLyLink{}, ids).Error; err != nil {
		return errors.New(e.ErrorInfo)
	} else {
		return nil
	}
}

// 获取城市
func (*FriendlyLink) GetCityList() []*friendly_link_model.CityItem {
	var list = make([]*friendly_link_model.CityItem, 0)
	var regionList []*admin.Region
	global.OC_DB.Preload("Districts").Where("level = ?", "province").Order("sort desc").Find(&regionList)
	for _, v := range regionList {
		var i = &friendly_link_model.CityItem{Name: v.Name, ID: v.ID, Level: 1}
		for _, j := range v.Districts {
			var i1 = &friendly_link_model.CityItem{Name: j.Name, ID: j.ID, Level: 2}
			i.Districts = append(i.Districts, i1)
		}
		list = append(list, i)
	}
	return list
}

var FriendlyLinkService = new(FriendlyLink)
